# configure_replication.pm
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
package CCNv2::Provision::Application::configure_replication;
use strict; use warnings;
use CCNCore::Provision::Base;
use base qw(CCNCore::Provision::Base);
use CCN::Text;
use CCNCore::Scripting;
use CCNCore::Home;

=pod
    configure_replication
    
        This module is about intra-cluster data replication: voicemail messages, prompts, etc.
        This is not about CDN downstream replication.
        
=cut

sub run()
{
    my $self = shift;
    # To make this work we first need to cross-authorize the servers in the same clusters.
    # This is done in distribute_keys.pm

    # --- Configure call-processing content synchronization
    if( defined $self->p->{cp_member} )
    {
        my @cp_servers = $self->sql_get_array(q(SELECT pk FROM server WHERE cp_member = ?),$self->p->{cp_member});

        # The prompt recording sequence terminates with
        #  exten => s,n,Exec(CCNCore::Home::manager."/bin/spool.pl replicate-cp-file ${PROMPTFILE}")
        # YYY Replace these with spool scripts that do the replication using the database.

        CAT(CCNCore::Home::config.'/local/bin/cp_servers_login',
            join("\0", map { "'root\@$_'" } grep { $_ != $self->p->server_pk } @cp_servers ));
    }

    # --- Configure voicemail content synchronization (messages, prompts)
    if( defined $self->p->{vm_member} )
    {
        my @vm_servers = grep { $_ != $self->p->server_pk } $self->sql_get_array(q(SELECT pk FROM server WHERE cp_member = ?),$self->p->{cp_member});
        CAT(CCNCore::Home::config.'/local/bin/vm_servers_login',
            join("\0", map { "'root\@$_'" } @vm_servers ));

        # Same as above except the list is a list of accounts with the primary voicemail server for them.
        # See voicemail.conf  in asterisk_voicemail.
        my $vmpath = $self->p->{vmpath};
        my $home_manager = CCNCore::Home::manager;
        for my $f (CCNCore::Home::config.'/local/bin/vm-notify.sh')
        {
            # Arguments to externnotify are: context extension newvoicemails
            CAT $f, copyright('#!/bin/sh').<<"EOT";
$home_manager/bin/spool.pl synchronize-vm "\$1" "\$2"
$home_manager/bin/spool.pl replicate-vm-file "${vmpath}/\$1/\$2/busy.WAV"
$home_manager/bin/spool.pl replicate-vm-file "${vmpath}/\$1/\$2/greet.WAV"
$home_manager/bin/spool.pl replicate-vm-file "${vmpath}/\$1/\$2/unavail.WAV"
EOT
            IF_CHANGED '' => "chmod +x $f";
        }
    }

    # --- Configure web content synchronization (themes, ...)

    # TBD-URGENT Web Content Synchronization
}
1;